package com.study.traincard;

import com.study.traincard.database.Form;
import com.study.traincard.database.FormDatabase;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentProvider;
import ohos.agp.utils.Color;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;


import java.util.*;


public class TimerAbility extends Ability {
    private static final long SEND_PERIOD = 60000L;
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "timer");
    Long formId = 0L;
    private Timer timer;
    private final DatabaseHelper helper = new DatabaseHelper(this);
    private OrmContext connect;

    public void onStart(Intent intent) {
        connect = helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class);
        HiLog.info(LABEL_LOG, "enter timer service");
        startTimer();
        super.onStart(intent);
    }

    // 卡片更新定时器，每分钟更新一次
    private void startTimer() {
        timer = new Timer();
        timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        updateForms();
                    }
                },
                0,
                SEND_PERIOD);
    }

    private void updateForms() {
        // 从数据库中获取卡片信息
        OrmPredicates ormPredicates = new OrmPredicates(Form.class);
        List<Form> formList = connect.query(ormPredicates);
        // 更新时分秒
        if (formList.size() <= 0) return;
        formId = formList.get(0).getFormId();

        Boolean isDataEmpty = formList.get(0).getIsDataEmpty();


        ComponentProvider componentProvider = new ComponentProvider(ResourceTable.Layout_form_image_with_info_widget_2_4, this);


        if (isDataEmpty) {
            Date journeyDate = formList.get(0).getJourneyDate();
            //判断日期输入提示
            Calendar rightnow = Calendar.getInstance();
            Calendar jd = Calendar.getInstance();
            jd.setTime(journeyDate);

            long between =(jd.getTimeInMillis() - rightnow.getTimeInMillis()) / (1000 * 3600 * 24) ;
            if (between > 1)
                componentProvider.setText(ResourceTable.Id_s1_journy_tips, "准备出行");
            else if (between == 1){
                componentProvider.setText(ResourceTable.Id_s1_journy_tips, "明天出行准备好了吗");
                componentProvider.setTextColor(ResourceTable.Id_s1_journy_tips, Color.GREEN);
            }
            else if (between == 0) {
                long bt = (jd.getTimeInMillis() - rightnow.getTimeInMillis()) / (1000 * 60);
                if (bt > 2 * 60)
                    componentProvider.setText(ResourceTable.Id_s1_journy_tips, "今天出行准备好了吗");
                else if (bt > 15)
                    componentProvider.setText(ResourceTable.Id_s1_journy_tips, "时间临近该出发了");
                else if (bt > 3) {
                    componentProvider.setText(ResourceTable.Id_s1_journy_tips, "开始检票上火车了");
                    componentProvider.setTextColor(ResourceTable.Id_s1_journy_tips, Color.RED);
                }
                else if(bt>0)
                    componentProvider.setText(ResourceTable.Id_s1_journy_tips, "已停止检票旅途愉快");
                else
                    componentProvider.setText(ResourceTable.Id_s1_journy_tips, "列车已出发旅途愉快");
            } else {
                componentProvider.setText(ResourceTable.Id_s1_journy_tips, "已到达下次旅途再见");

                terminateAbility();
            }
        }



        try {
            updateForm(formId, componentProvider);
        } catch (FormException e) {
            HiLog.error(LABEL_LOG, "onUpdateForm updateForm error");
        }

    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LABEL_LOG, "TimerAbility::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        timer.cancel();

        HiLog.info(LABEL_LOG, "TimerAbility::onStop");
    }
}
